import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Delete from '#cpt/temp/Delete'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '模板名称',		name: 'temp_name' },
		],
		// 提交表单列表
		formData : [
			{ label: '模板名称',			name: 'temp_name', 		full:true, 	required:true },
			{ label: '备注',			name: 'remark', 		full:true },
			{ label: '模板文件',			name:'temp_url', full:true, type:'upload', mode:'button', btnText:'上传模板', imports:true, ghost:false, btnConfig:{size:'middle'},
				api:'conf-import-temp/import',
				fileType:'excel',
			},
		]
	}
	
	model = {}
	
	componentDidMount(){
		let { skip } = $fn.query()
		if(skip){ this.skip = skip }
		this.fetch()
	}

	// paging
	fetch = () => $fn.fetch.call(this,'conf-import-temp/index')
	
	// 表单
	cols = [
		{ title: '模板名称',		dataIndex: 'temp_name', 		width:200 },
		{ title: '创建时间', 	dataIndex: 'create_at', 		...$fn.table.time },
		{ title: '更新时间', 	dataIndex: 'update_at', 		...$fn.table.time },
		{ title: '备注', 		dataIndex: 'remark', 			width:200 },
		{ title: '当前版本',		dataIndex: 'version_num', 		width:100 },
		TableControls.controls.call(this,{ width: 150 })
	]
	// 表格操作按钮
	table_controls = (text,rows) => {
		const { uuid, temp_name, temp_url } = rows
		return [
			TableControls.edit.call(this, null, null, { rows, click:()=>{ this.submit = { temp_type: 0 } } }),
			this.onDel([uuid]),
			TableControls.pushTo.call(this, '版本', `check-version?uuid=${uuid}&temp_name=${$fn.encode(temp_name)}`),
			Controls.click.call(this,'下载',()=> window.location.href=temp_url, { disabled:this.temp_url }),
		]
	}
	// 操作按钮
	controls = () => {
		return [
			// 添加
			Controls.add.call(this, { click: ()=>{
				const { formData } = this.state
				this.setState({ formData })
				this.submit = { temp_type: 0 }
			} }),
			// 删除
			this.onDel(),
			// 返回
			this.skip && Controls.back.call(this),
		]
	}
	
	// 删除
	onDel = uuid => Delete.call(this, 'conf-import-temp/delete', { uuid, idStr:'uuids', isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='导入模板' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'conf-import-temp/add', editApi:'conf-import-temp/edit'  }) }
			</>
		)
	}
}

export default Index